package com.hnust.controller;

import com.hnust.domain.record.CollegeRecord;
import com.hnust.service.CollegeService;
import com.hnust.service.TokenService;
import com.hnust.util.Result;
import net.sf.json.JSONArray;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *创建人：高富昌
 *创建时间：2020/10/7
 *描述：学院管理模块
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/college")
public class CollegeController {

    @Autowired
    private CollegeService collegeService;

    @Autowired
    private TokenService tokenService;


    /**
     * 获取所有学院信息
     * @param token token信息
     * @return
     */
    @GetMapping("/get")
    public Result<List<CollegeRecord>> collegeGet(
            @RequestParam("token") String token
    ) {
        //验证token是否正确
        if (tokenService.verifyToken(token, "校管理员")) {
            try{
                List<CollegeRecord> collegeRecords = collegeService.collegeGet();
                return new Result<>(1, "获取学院成功", collegeRecords);
            }catch (Exception e){
                return new Result<>(0, "获取学院失败", null);
            }
        }
        else{
            return new Result<>(0, "没有操作权限,请登录", null);
        }
    }


    /**
     * 添加学院
     * @param map token token信息,collegeId 学院Id,name 学院名
     * @return
     */
    @PostMapping("/add")
    public Result<String> collegeAdd(@RequestBody Map<String,String> map)
    {
        String token = map.get("token");
        String name = map.get("name");

        //验证token是否正确
        if (tokenService.verifyToken(token, "校管理员")) {
            try {
                String result = collegeService.collegeAdd(name);
                return new Result<>(1, "添加学院成功", result);
            } catch (Exception e) {
                return new Result<>(0, "添加学院失败", null);
            }
        }
        else {
            return new Result<>(0, "没有操作权限,请登录", null);
        }
    }


    /**
     * 删除学院
     * @param token token信息
     * @param collegeId 学院Id
     * @return
     */
    @GetMapping("/delete")
    public Result<String> collegeDelete(
            @RequestParam("token") String token,
            @RequestParam("collegeId") String collegeId
    ) {
        //验证token是否正确
        if (tokenService.verifyToken(token, "校管理员")){
            if(collegeService.collegeDelete(collegeId)) {
                return new Result<>(1, "操作成功", null);
            }
            else{
                return new Result<>(0,"操作失败",null);
            }
        }else {
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 批量删除学院
     * @param map token token信息，collegeIds 学院Ids
     * @return
     */
    @PostMapping("/batchDel")
    public Result<String> collegeBatchDelete(@RequestBody Map<String,Object> map)
    {
        String token = map.get("token").toString();
        JSONArray collegeIds= JSONArray.fromObject(map.get("collegeIds"));

        //验证token是否正确
        if (tokenService.verifyToken(token, "校管理员")){
            if(collegeService.collegeBatchDelete(collegeIds)){
                return new Result<>(1,"操作成功",null);
            }else{
                return new Result<>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    @GetMapping("/getAdminInfo")
    public Result<Map<String,String>> collegeAdminInfo(
            @RequestParam("token") String token,
            @RequestParam("colAdmId") String colAdmId
    ) {
        //验证token是否正确
        if (tokenService.verifyToken(token, "校管理员")){
            try {
                return new Result<Map<String,String>>(1,"操作成功",collegeService.getCollegeAdminInfo(colAdmId));
            }catch (Exception e){
                return new Result<Map<String,String>>(0,"操作失败",null);
            }
        }else {
            return new Result<Map<String,String>>(0,"没有操作权限,请登录",null);
        }
    }
}